exports.controllers={
	/*
		Function: controllers.infoblocks
	
		Makes an infoblocks.
			
		Parameters:
			options - an object of:
			options.group_id - optional, id from Tinfoblock_groups
			options.group_code - optional, code from Tinfoblock_groups
			options.group_name - optional, name from Tinfoblock_groups - need one of "group_id" "group_code" or "group_name"
			options.template - optional, template to render, defaults to "infoblocks/body"
			options.preview - optional, previews size, defaults to null
			options.limit - optional, count of infoblocks, defaults to 3
		Returns:
			Valid HTML
	*/
	infoblocks: function(space,options) {
		var grpid=space.functions.get_infoblock_group_id(space,options,"group");
		space.infoblocks=space.mfact.list("infoblock",{group_id: grpid,order_by: "main.ordering",limit: options.limit||3,enabled:1});
		if (options.preview) {
			space.infoblocks.forEach(function(x) {
				x.image_id_url=space.functions.preview(space,options.preview,x.image_id_url);
			});
		}
		return space.views.process("widgets/"+(options.template||"infoblocks/body"),space);
	}
};

exports.functions={
	/*
		Function: functions.get_infoblock_group_id

		Gets group_id from either one - code, name or id of infoblock_groups

		Parameters:
			options - the hash of:
			options.PREFIX_id - optional, from Tinfoblock_groups table
			options.PREFIX_code - optional, from Tinfoblock_groups table
			options.PREFIX_name - optional, from Tinfoblock_groups table
			prefix - a prefix to use in search of options

		Returns:
			Numeric ID or undefined
	*/
	get_infoblock_group_id:function(space,options,prefix) {
		if (!prefix) prefix="nav";
		if (options[prefix+"_id"]) return options[prefix+"_id"];
		if (options[prefix+"_code"]) return space.site.sql.execute_and_fetch_one("infoblock_groups/get_by_code",{code: options[prefix+"_code"]}).id;
		if (options[prefix+"_name"]) return space.site.sql.execute_and_fetch_one("infoblock_groups/get_by_name",{name: options[prefix+"_name"]}).id;
		return undefined;
	}
};
